<?php
require_once 'FL/CMS.php';
require_once 'FL/List/Interface.php';

abstract class FL_List_Abstract extends FL_CMS implements FL_List_Interface {
	protected $i_list_id;
	protected $i_type_id;
	protected $i_cluster_id;
	protected $s_table_name;
	
	protected $s_sql_sheme = '';
	
	abstract protected function _getScheme(); 
	
	public function __construct($i_list_id, $i_type_id, $i_cluster_id){
		$this->i_list_id = $i_list_id;
		$this->i_type_id = $i_type_id;
		$this->i_cluster_id = $i_cluster_id;
		
		$this->s_table_name = $this->_getListTableName($this->i_list_id);
	}
	
	public function regItem($a_item_data){
		$a_item_data['register_list_id'] = $this->i_cluster_id;		
		self::$_o_dba->queryInsert($this->s_table_name, $a_item_data);
		return self::$_o_dba->nextId();
	}
	
	public function unregItem($i_item_id){		
		self::$_o_dba->queryDelete($this->s_table_name, 'item_id', $i_item_id);
	}
	
	public function assignTagToItem($i_item_id, $i_tag_id){
		$a_values = array(
			'register_list_id' 	=> $this->i_list_id,
			'list_item_id' 		=> $i_item_id,
			'tags_tag_id'		=> $i_tag_id
		);		
		self::$_o_dba->queryInsert(self::T_L_LIST_TAGS, $a_values);
		return self::$_o_dba->nextId();
	}
	
	public function unassignTagFromItem($i_item_id, $i_tag_id){
		$s_sql =  'DELETE FROM #' . self::T_L_LIST_TAGS 
				. '  WHERE register_list_id = ' . $this->i_list_id
				. '    AND tags_tag_id = ' . $i_tag_id
				. '    AND list_item_id = ' . $i_item_id;
		$o_result = self::$_o_dba->query($s_sql);
	}
	
	public function setItemProps($i_item_id, $a_item_data){
		self::$_o_dba->queryUpdate($this->s_table_name, 'item_id', $i_item_id, $a_item_data);
	}
	
	//TODO: implement search by tags and link rules
	public function getItems($i_page, $a_tags_ids = array()){		
		$a_limits = $this->_getLimits($i_page);
		$s_sql =  'SELECT * FROM #' . $this->s_table_name . ' AS l '
				. (is_array($a_tags_ids) && !empty($a_tags_ids)
					? '  JOIN #' . self::T_L_LIST_TAGS . ' AS tl ON tl.list_item_id = l.item_id'
					. '    AND tl.tags_tag_id IN ('. implode($a_tags_ids) . ')' : '')
				. ' LIMIT '  . $a_limits['limit']
				. ' OFFSET ' . $a_limits['offset'];
		$o_result = self::$_o_dba->query($s_sql);
		return $o_result->fetchAllAsArray();
	}
	
	public function install(){			
		if(!self::$_o_dba->isTableExists($this->s_table_name)){
			$s_sql_scheme = $this->_getScheme();
			$s_sql_scheme = str_replace('#table_name', '#' . $this->s_table_name, $this->s_sql_sheme);
			self::$_o_dba->query($s_sql_scheme);
		}
	}
	
	/*
	 * Utilities
	 */
	protected function _generateNewListScheme($a_new_fields){
		$s_sql_scheme  = 'CREATE  TABLE IF NOT EXISTS #table_name (
		  item_id INT NOT NULL AUTO_INCREMENT ,
		  list_links_link_id INT NULL ,
		  register_list_id INT NOT NULL ,';
		
		$s_sql_scheme .= implode(",\n", $a_new_fields);
		
		$s_sql_scheme .= ', PRIMARY KEY (item_id) ,
		  INDEX fk_weblog_list_links1 (list_links_link_id ASC) ,
		  INDEX fk_weblog_register1 (register_list_id ASC) ,
		  CONSTRAINT fk_weblog_list_links1
		    FOREIGN KEY (list_links_link_id )
		    REFERENCES #' . self::T_L_LIST_TAGS . ' (link_id )
		    ON DELETE NO ACTION
		    ON UPDATE NO ACTION,
		  CONSTRAINT fk_weblog_register1
		    FOREIGN KEY (register_list_id )
		    REFERENCES #' . self::T_REGISTER . ' (list_id )
		    ON DELETE NO ACTION
		    ON UPDATE NO ACTION)';
		
		return $s_sql_scheme;
	} 
} 